/**
 * 观察者订阅中心
 * @author GuoHua Liu
 * @copyright Copyright © 2019 GuoHua Liu. All rights reserved.
 */
class Observer {
    constructor() {
        this.__observerss = {}
    }
    /**
     * 添加观察者
     * @param {String} name 插件名称
     * @param {*} context onevent函数所在的上下文
     * @param {Function} onevent 观察者函数
     */
    addObserver(name, context, onevent) {
        if (typeof onevent != "function") {
            let ctx = onevent
            onevent = context
            context = ctx
        }
        if (typeof onevent != "function") {
            console.error("参数错误")
        }

        let observer = {"onevent":onevent, "context":context}

        let observers = this.__observerss[name]
        if (!observers) {
            this.__observerss[name] = [observer]
        } else {
            this.removeObserver(name, onevent) // 避免多次添加
            observers.push(observer)
        }
    }
    /**
      * 发射事件
      * @param {String} name
      * @param {Object} data
      * @param {Number} id
      */
     fireEvent(name, data, id=0) {
          // console.log("发射事件", name, id, data)
          let observers = this.__observerss[name]
          if (observers) {
               (function () {
                    let i = observers.length
                    while(i--){
                         let observer = observers[i]
                         if(!observer || !observer.onevent){
                              // console.log("清理无效观察者", observer, observers.length)
                              observers.splice(i,1)
                         }
                    }
               })()

               for (let index in observers) {
                    let observer = observers[index]
                    // console.log("观察者", observer, "执行函数", observer.onevent, "上下文", observer.context)
                    if (observer.context) {
                         // 用户指定了执行上下文
                         observer.onevent.call(observer.context, {"name":name, "id":id, "data":data})
                    } else {
                         observer.onevent({"name":name, "id":id, "data":data})
                    }
               }
          }
     }

    /**
     * 遍历所有观察者
     * @param name 指定事件名称，如果没有指定，将遍历所有事件的观察者
     * @param block
     */
     forEach(block, name = null) {
         if (name) {
             let observers = this.__observerss[name]
             if (observers) {
                 for (let index in observers) {
                     const observer = observers[index];
                     block(name, observer.onevent, observer.context)
                 }
             }
         } else {
             for (let name in this.__observerss) {
                 let observers = this.__observerss[name]
                 if (observers) {
                     for (let index in observers) {
                         const observer = observers[index];
                         block(name, observer.onevent, observer.context)
                     }
                 }
             }
         }
     }

     /**
      * 删除观察者
      * @param {String} name 事件名称
      * @param {*} onevent 观察函数，如果为null，将移除之前添加的所有函数
      */
     removeObserver(name, onevent=null) {
          let observers = this.__observerss[name]
          if (observers) {
              if (!onevent) {
                  this.__observerss[name] = []
                  return
              }
              for(let index in observers) {
                  if (observers.hasOwnProperty(index)) {
                      const observer = observers[index];
                      if(observer.onevent === onevent) {
                          observer.onevent = undefined;
                          observer.context = undefined;
                          observers.splice(index, 1);
                          break
                      }
                  }
              }
          }
     }
}

export {Observer}
